<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\ValidationCode\Service;

use MoChat\App\ValidationCode\Contract\ValidationCodeContract;
use MoChat\App\ValidationCode\Model\ValidationCode;
use MoChat\Framework\Service\AbstractService;

class ValidationCodeService extends AbstractService implements ValidationCodeContract
{
    /**
     * @var ValidationCode
     */
    protected $model;

    /**
     * 查询单条 - 根据手机号.
     * @param string $phone 手机号
     * @param int $captcha 验证码
     * @param int $type 验证码
     * @param array $columns 查询字段
     */
    public function getValidationCodeByPhoneCaptchaType(string $phone, int $captcha, int $type, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('phone', $phone)
            ->where('status', 0)
            ->where('type', $type)
            ->where('captcha', $captcha)
            ->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createValidationCode(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 修改验证码状态 - 根据id.
     * @param int $status 账户状态
     * @return int 修改条数
     */
    public function updateValidationCodeStatusById(int $id, int $status): int
    {
        return $this->model::query()->where('id', $id)->update(['status' => $status]);
    }
}
